package cn.shop.dys;

import android.app.Application;
import android.text.TextUtils;

import org.jivesoftware.smack.XMPPException;

import c.b.BP;
import cn.shop.dys.constant.PayConstants;
import cn.shop.dys.util.ConnectionManager;
import cn.shop.dys.util.MyDBUtil;
import cn.shop.lib_common.CommonUtil;

/**
 * Created by sen on 2016/4/25 0025.
 */
public  class DYShopApplication extends Application {
    private ConnectionManager manager;
    @Override
    public void onCreate() {
        super.onCreate();
        MyCrashHandler handler = MyCrashHandler.getInstance();
        Thread.setDefaultUncaughtExceptionHandler(handler);
        MyDBUtil.initDB(this,"shop_chat.db");
        manager = ConnectionManager.getInstance();
        startLoad();
        BP.init(this, PayConstants.APPID);
    }
    private void startLogin() {
        String accountStr = "admin";
        String pwdStr = "1";
        if(TextUtils.isEmpty(accountStr)||TextUtils.isEmpty(pwdStr)){
            CommonUtil.showToast(this, "账号或密码不能为空");
            return;
        }
        try {
            manager.login(this,accountStr,pwdStr);
        } catch (XMPPException e) {
            CommonUtil.showSingleToast(this, e.getMessage());
        }
    }
    private void startLoad() {
        CommonUtil.runOnThread(new Runnable() {
            @Override
            public void run() {
                String ip="192.168.56.1";
                try {
                    if (!manager.isConnected()) {
                        manager.connect(ip);
                    }
                    if (!manager.isLogin()) {
                        startLogin();
                    }
                } catch (XMPPException e) {
                    e.printStackTrace();
                 //   CommonUtil.showSafeToast(UserServiceActivity.this, e.getMessage());
                }
            }
        });
    }
}
